package com.hdaccp.hadoop.mr1;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;

/*
//音乐倒序排列
 * 分别使用到了
KeyValueTextInputFormat
SequenceFileInputFormat
SequenceFileOutputFormat

 * 
 */
public class TopMusic {

	public static class CounterMapper extends
			Mapper<Text, Text, Text, IntWritable> {

		@Override
		public void map(Text key, Text value, Context context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			context.write(key, new IntWritable(1));
		}
	}

	public static class CounterReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		@Override
		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			context.write(key, new IntWritable(sum));
		}

	}

	/**
	 * 自定义分区
	 */
	public static class Partition extends Partitioner<Text, IntWritable> {
		@Override
		public int getPartition(Text key, IntWritable value, int arg2) {
			// TODO Auto-generated method stub
			String song = key.toString().substring(4);
			if (Integer.parseInt(song) < 5)
				return 0;
			else
				return 1;
		}

	}

	/**
	 * 实现降序
	 */
	static class SortComparator extends WritableComparator {

		protected SortComparator() {
			super(IntWritable.class, true);//IntWritable
		}

		@Override
		public int compare(WritableComparable a, WritableComparable b) {
			return -super.compare(a, b);
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Configuration conf = new Configuration();
		conf.addResource("mapred-site.xml");
		// 默认\t
		conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR, "\t");
		try {
			FileSystem.get(conf).delete(new Path(args[1]), true);// 调试使用
			Job job = Job.getInstance(conf, "music count");
			job.setJarByClass(TopMusic.class);
			job.setNumReduceTasks(2);
			job.setPartitionerClass(Partition.class);

			job.setMapperClass(CounterMapper.class);
			job.setReducerClass(CounterReducer.class);

			job.setInputFormatClass(KeyValueTextInputFormat.class);
			job.setOutputFormatClass(SequenceFileOutputFormat.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(IntWritable.class);

			FileInputFormat.addInputPath(job, new Path(args[0]));
			FileOutputFormat.setOutputPath(job, new Path(args[1]));

			if (job.waitForCompletion(true)) {
				Job topSort = Job.getInstance(conf, "top music");
				topSort.setNumReduceTasks(1);
				topSort.setJarByClass(TopMusic.class);
			

				topSort.setMapperClass(InverseMapper.class);
				// Map输入格式
				topSort.setInputFormatClass(SequenceFileInputFormat.class);

				topSort.setOutputKeyClass(IntWritable.class);
				topSort.setOutputValueClass(Text.class);

				FileInputFormat.addInputPath(topSort, new Path(args[1]));
				FileOutputFormat.setOutputPath(topSort, new Path(args[1]
						+ "/top"));
				topSort.setSortComparatorClass(SortComparator.class);// 实现降序
				System.exit(topSort.waitForCompletion(true) ? 0 : 1); // System.exit(0)是正常退出程序，而System.exit(1)或者说非0表示非正常退出程序
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
